#include "../head/LoginForm.h"
#include "ui_LoginForm.h"
#include "../head/mainwindow.h"
#include "../head/utils/BMachineControl.h"

LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent),


    ui(new Ui::LoginForm)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint|Qt::CustomizeWindowHint);

}

void LoginForm::setWinTitle(QString title){
    this->setWindowTitle(title);
    strTitle = title;
    //BMachineControl *bc = new BMachineControl();
   // ui->lineEdit->setText(bc->getCrypKey(ui->lineEdit_2->text()));
}

void LoginForm::setParams(QString macAddress,bool status){
    ui->lineEdit_2->setText(macAddress);
    ui->lineEdit_2->setReadOnly(true);
    ui->btnTestVersion->setEnabled(status);
}


void LoginForm::setCritical(QString errorMessage){
    QMessageBox::critical(this,"提示",errorMessage);
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::on_pushButton_2_clicked()
{

}


void LoginForm::on_btnTestVersion_clicked()
{
    MainWindow *mainWin = new MainWindow();
    mainWin->show();
    mainWin->setWinTitle(strTitle);
    mainWin->setComSerial();
    this->hide();
}


//b5de3f8dc480a33820f6398ee239bec1f3e0f964
void LoginForm::on_btnRegEntry_clicked()
{
    BMachineControl *bc = new BMachineControl();
     //ui->lineEdit->setText(bc->getCrypKey(ui->lineEdit_2->text()));
    QString encpy1 = bc->getCrypKey(ui->lineEdit_2->text());
    QString encpy2 = bc->getCrypKey1(ui->lineEdit_2->text());
    QString encpy = ui->lineEdit->text();

    if(encpy == encpy1){ //注册更新一个月;
        QString test_time_out = bc->writeTempRegVal();
        MainWindow *mainWin = new MainWindow();
        mainWin->show();
        mainWin->setWinTitle("电磁波解码器V2.0【试用到期时间"+test_time_out+"】");
        mainWin->setComSerial();
        this->hide();
    }else if(encpy == encpy2){ //永久注册
        bc->writeRegVal("encry",encpy1);
        MainWindow *mainWin = new MainWindow();
        mainWin->show();
        mainWin->setWinTitle("电磁波解码器V2.0");
        mainWin->setComSerial();
        this->hide();
    }else{
        QMessageBox::critical(this,"提示","注册码错误");
    }
}
